package cc.dd.marketing.activity.parser;

import cc.dd.marketing.core.AbstractRequestDTOParser;
import cc.dd.marketing.core.annotation.ActivityTypeMapper;
import cc.dd.marketing.core.annotation.FunctionMapper;
import cc.dd.marketing.core.base.ResponseParser;
import cc.dd.marketing.activity.dto.PullPInnerDTO;
import cc.dd.marketing.activity.rule.domain.ActivityStatusRule;
import cc.dd.marketing.activity.rule.domain.EndTimeRule;
import cc.dd.marketing.activity.rule.domain.StartTimeRule;
import cc.dd.marketing.api.constant.ActivityTypeEnum;
import cc.dd.marketing.api.constant.FunctionCodeEnum;
import cc.dd.marketing.api.request.activity.ActivityParticipateReqDTO;
import cc.dd.marketing.api.response.activity.ActivityCommonResponseDTO;
import cc.dd.marketing.api.response.base.BaseResponse;
import cc.dd.marketing.common.ActivityStatusEnum;
import cc.dd.marketing.common.ContextParam;
import cc.dd.marketing.common.RewardDistributionProgressStatusEnum;
import cc.dd.marketing.entity.ActivityConfig;
import cc.dd.marketing.rule.base.Rule;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 拉人活动实体组装
 *
 * @author dongsufeng
 * @date 2022/9/9 2:29 PM
 */
@Component
@ActivityTypeMapper({ActivityTypeEnum.P_PULL_P})
@FunctionMapper({FunctionCodeEnum.PARTICIPATE})
@Slf4j
public class PullPActivityParser extends AbstractRequestDTOParser<ActivityParticipateReqDTO, PullPInnerDTO> implements ResponseParser<ActivityParticipateReqDTO, PullPInnerDTO> {


    @Override
    protected List<Rule> buildRules(PullPInnerDTO innerDTO) {
        log.info("activity do buildRules");
        List<Rule> rules = new ArrayList<>();
        rules.add(new StartTimeRule(innerDTO.getActivityConfig().getStartTime()));
        rules.add(new EndTimeRule(innerDTO.getActivityConfig().getEndTime()));
        rules.add(new ActivityStatusRule(innerDTO.getActivityConfig().getStatus()));
        return rules;
    }

    @Override
    protected PullPInnerDTO assembleDTO(ActivityParticipateReqDTO request, PullPInnerDTO innerDTO) {
        log.info("activity do assembleDTO");
        if (Objects.isNull(innerDTO)) {
            innerDTO = new PullPInnerDTO();
        }
        //查询数据库
        ActivityConfig activityConfig = new ActivityConfig();
        activityConfig.setStartTime(DateUtils.addDays(new Date(),-1));
        activityConfig.setEndTime(DateUtils.addDays(new Date(),+1));
        activityConfig.setStatus(ActivityStatusEnum.PROCESSING.getCode());
        innerDTO.setActivityConfig(activityConfig);
        return innerDTO;
    }

    @Override
    public BaseResponse buildResponse(ContextParam<ActivityParticipateReqDTO, PullPInnerDTO> contextParam) {
        log.info("do buildResponse");
        ActivityCommonResponseDTO responseDTO = new ActivityCommonResponseDTO();
        responseDTO.setActivityCode(contextParam.getRequest().getActivityCode());
        responseDTO.setStatus(RewardDistributionProgressStatusEnum.PARTICIPATE.getStatus());
        responseDTO.setSchedule(RewardDistributionProgressStatusEnum.PARTICIPATE.getDesc());
        responseDTO.setChannelNo(contextParam.getRequest().getChannelNo());
        responseDTO.setUserId(contextParam.getRequest().getUserId());
        responseDTO.setRecommendUserId(contextParam.getBaseDTO().getActivityUser() != null ? contextParam.getBaseDTO().getActivityUser().getUserId() : "0");
        return responseDTO;
    }
}
